/*
* name;
*/
export default class DBMgr{

    public static parse() {
        let str = window['pako'].ungzip(cc.resources.get("data/db")['text'], { to: 'string' });
        let resData = JSON.parse(str);
        this.data = resData;
    }

    public static data: any;
    public static getLang(id: number, ...args) {
        if (args.length == 0) {
            return this.data.langDB[id].content;
        }
        // return StrUtils.replace.apply(null,[this.data.langDB[id].content].concat(args));
        return this.formatContent(this.data.langDB[id].content, args);
    }

    public static getTable(name) {
        if (!DBMgr.data[name]) { 
            console.error("获取不到表:"+name);
            return {};
        } else {
            return DBMgr.data[name];
        }
        
    }

    public static getTableRow(name, id) {
        if (!this.getTable(name)[id]) {
            console.error("获取不到表:"+name+" id:"+id);
            return {};
        } else {
            return this.getTable(name)[id];
        }
    }

    public static getTableData(name, id, key) {
        if (this.getTableRow(name, id)[key] == null) {
            console.error("获取不到表:"+name+" id:"+id+" key:"+key);
            return null;
        } else {
            return this.getTableRow(name, id)[key];
        }
    }

    private static _pattern: RegExp = /\{(.*?)\}/g;
    /**
     * 
     * @param str 需要格式化的字符串
     * @param args 格式化的参数
     */
    public static formatContent(str: string, args): string {
        if (args && args.length > 0) {
            var arr: Array<string> = str.match(this._pattern);
            if (arr.length != args.length) {
                console.warn("填充的参数个数不匹配：" + str);
            }
            for (var i: number = 0; i < arr.length; i += 1) {
                switch (arr[i]) {
                    case "{item}":
                        // str = str.replace(arr[i],PropsModel.instance.itemVos[args[i]].colorName);
                        break;
                    case "{equip}":
                        // str = str.replace(arr[i],PropsModel.instance.equipVos[args[i]].colorName);
                        break;
                    default:
                        str = str.replace(arr[i], args[i]);
                        break;
                }
            }
        }
        return str;
    }
}